package main

import (
  "encoding/json"
  //"fmt"
  "io/ioutil"
  "os"
)

type MysqlOPT struct {
  Host string `json:"host"`
  Port int `json:"port"`
  User string `json:"user"`
  Passwd string `json:"passwd"`
  DBName string `json:"dbname"`
}

type SQLite3OPT struct {
  DBFile string `json:"dbfile"`
  DBName string `json:"dbname"`
}

type Config struct {
  PersistenceType int `json:"persistencetype"`        //持久化方式，1--mysql，2--sqlite3
  MysqlOpt MysqlOPT `json:"mysqlopt"`                 //mysql参数
  Sqlite3Opt SQLite3OPT `json:"sqlite3opt"`           //sqlite3参数
}

var conf Config

func conf_init() {
  var config_path string
  config_path = getEnvDefault("CONFIG_PATH", ".")
  data, err := ioutil.ReadFile(config_path + "/config.json")
  if err != nil {
    //fmt.Println("ReadFile failed", err)
    panic(err)
  }
  json.Unmarshal(data, &conf)
}

func getEnvDefault(key, defVal string) string {
  val, ex := os.LookupEnv(key)
  if !ex {
    return defVal
  }
  return val
}
